package be.novelfaces.component.numpaddecsep;

import static be.novelfaces.component.util.attribute.AttributeType.ID;
import static be.novelfaces.component.util.element.ElementType.SPAN;

import java.io.IOException;

import javax.faces.application.ResourceDependencies;
import javax.faces.application.ResourceDependency;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import org.richfaces.cdk.annotations.JsfRenderer;

import be.novelfaces.component.encoder.Encoder;

@ResourceDependencies({
	@ResourceDependency(library = "novelfaces", name = "js/jquery-nf.min.js"),
	@ResourceDependency(library = "novelfaces", name = "js/jquery.numpadDecSeparator-1.1.2-min.js"),
	@ResourceDependency(library = "novelfaces", name = "js/novelfaces.js") })
@JsfRenderer(family = "be.novelfaces.component", //
type = "be.novelfaces.component.NumpadDecimalSeparatorRenderer")
public class NumpadDecimalSeparatorRenderer extends Renderer {
	
	private Encoder jsEncoder = new NumpadDecimalSeparatorJSEncoder();
	
	@Override
	public void encodeBegin(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement(SPAN.getName(), null);
		writer.writeAttribute(ID.getName(), component.getClientId(), null);
	}
	
	@Override
	public void encodeEnd(FacesContext context, UIComponent component)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		jsEncoder.encode(context, component);
		writer.endElement(SPAN.getName());
	}
	
	@Override
	public boolean getRendersChildren() {
		return true;
	}
	
}
